package com.demo.lifeinfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.demo.lifeinfo.app.XBaseActivity;
import com.demo.lifeinfo.common.home.fm.HomeFm;
import com.demo.lifeinfo.common.home.fm.MineFm;
import com.demo.lifeinfo.common.publish.PublishActivity;
import com.demo.lifeinfo.common.search.SearchAc;
import com.demo.lifeinfo.common.sign.LoginActivity;
import com.demo.lifeinfo.widget.BottomSectorMenuView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MainActivity extends XBaseActivity {

    @BindView(R.id.pageNavigationView)
    PageNavigationView pageNavigationView;

    NavigationController navigationController;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    BottomSectorMenuView bottomSectorMenuView;

    FragmentManager fm;
    List<Fragment> fragments;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {

        fragments = new ArrayList<>();
        fragments.add(HomeFm.newInstance());
        fragments.add(MineFm.newInstance());
        navigationController = pageNavigationView.material()
                .addItem(R.drawable.ic_home, "")
                .addItem(R.drawable.ic_mine, "")
                .build();
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                if (index == old) return;
                switchFm(index, old);
            }

            @Override
            public void onRepeat(int index) {

            }
        });
        fm = getSupportFragmentManager();
        fm.beginTransaction().add(R.id.content_view, fragments.get(0)).commitNowAllowingStateLoss();

        bottomSectorMenuView = new BottomSectorMenuView.Converter(fab)
                .setToggleDuration(500, 800)
                .setAnchorRotationAngle(135f)
                .addMenuItem(R.drawable.ic_feiji, "发布", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (BmobUser.isLogin()) {

                            startActivity(new Intent(MainActivity.this, PublishActivity.class));
                        } else {
                            startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        }
                    }
                })
                .addMenuItem(R.drawable.ic_serch, "搜索", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(MainActivity.this, SearchAc.class));
                    }
                })
                .apply();
    }

    @Override
    protected void initData() {

    }


    @OnClick(R.id.fab)
    public void onViewClicked() {
        if (bottomSectorMenuView == null) {

//            bottomSectorMenuView.openMenu();
        } else {
            if (bottomSectorMenuView.isOpen()) {
                bottomSectorMenuView.closeMenu(new BottomSectorMenuView.OnMenuClosedListener() {
                    @Override
                    public void onMenuClosed() {

                    }
                });
            } else {
                bottomSectorMenuView.openMenu();
            }
        }

    }

    private void switchFm(int index, int old) {

        if (fragments.get(index).isAdded()) {
            fm.beginTransaction().show(fragments.get(index)).hide(fragments.get(old)).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commitNowAllowingStateLoss();
        } else {
            fm.beginTransaction().add(R.id.content_view, fragments.get(index)).hide(fragments.get(old)).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commitNowAllowingStateLoss();
        }
    }


}
